import { useMassage } from '../ntils/hooks'

let { createMessage } = useMassage()
export function ClipboardData(str:any, target = document.body) {
    if (str.length < 1) {
        createMessage.error('复制内容不能为空')
        return false
    }
    let Domes = document.getElementsByClassName('TEXT_Symol')[0]
    let element:HTMLElement = Domes ?? document.createElement('textarea')
    element.value = str
    element.className = 'TEXT_Symol'
    element.setAttribute('readonly', '');
    (element.style as any).contain = 'strict'
    element.style.position = 'absolute'
    element.style.left = '-9999px'
    element.style.fontSize = '12pt'
    target.append(element)
    element.select()
    // 将textarea 的内容放到剪切板
    let isSuccess = document.execCommand('copy')
    const selection = document.getSelection()
    if (selection) {
        selection.removeAllRanges()
    }
    if (isSuccess) {
        createMessage.success('复制成功')
    }
    return isSuccess
}